package com.baobao.common.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired(required = false)
    private StringRedisTemplate redisTemplate;



    public void set(String key, String value) {
        this.redisTemplate.opsForValue().set(key, value);
    }



    public void setex(final String key, final String value, final Integer seconds) {
        this.redisTemplate.opsForValue().set(key, value);
        this.redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
    }


    public String get(String key) {
        return this.redisTemplate.opsForValue().get(key);
    }


    public void del(String key) {
        this.redisTemplate.delete(key);
    }


    public void expire(final String key, final Integer seconds) {
        this.redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
    }
}
